package carassius.DAL;

import carassius.BLL.Events.WaterRefreshment.WaterRefreshmentAddedListener;
import carassius.BLL.Events.WaterRefreshment.WaterRefreshmentAddedPublisher;
import carassius.BLL.Events.WaterRefreshment.WaterRefreshmentRemovedListener;
import carassius.BLL.Events.WaterRefreshment.WaterRefreshmentRemovedPublisher;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author siebz0r
 */
public class WaterRefreshmentTable
		extends ActionTable<WaterRefreshmentRow>
		implements WaterRefreshmentAddedPublisher,
				   WaterRefreshmentRemovedPublisher
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final ArrayList<WaterRefreshmentAddedListener> _waterRefreshmentAddedListeners;
	private final ArrayList<WaterRefreshmentRemovedListener> _waterRefreshmentRemovedListeners;
	private final String SQL_ADD = "INSERT INTO water_refreshment VALUES ( ? )",
			SQL_DELETE = "DELETE FROM water_refreshment WHERE id = ?",
			SQL_GET_ALL = "SELECT * FROM action NATURAL JOIN water_refreshment",
			SQL_GET_BY_ID = "SELECT * FROM action NATURAL JOIN water_refreshment WHERE id = ?",
			SQL_GET_BY_AQUARIUM_ID = "SELECT * FROM action NATURAL JOIN water_refreshment WHERE aquarium_id = ?";
	private boolean _allCached;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public WaterRefreshmentTable()
	{
		_waterRefreshmentAddedListeners = new ArrayList<WaterRefreshmentAddedListener>();
		_waterRefreshmentRemovedListeners = new ArrayList<WaterRefreshmentRemovedListener>();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	//<editor-fold defaultstate="collapsed" desc="SQL add">
	@Override
	public Integer add(Object[] args) throws SQLException
	{
		Integer out = null;
		out = super.add(args);
		if (out != -1)
		{
			synchronized (getRows())
			{
				PreparedStatement st = Database.prepareStatement(SQL_ADD);
				st.setInt(1, out);

				try
				{
					if (st.executeUpdate() == 1)
					{
						_allCached = false;
						ActionRow actionRow = getByPrimaryKey(out);
						_allCached = true;
						fireWaterRefreshmentAdded((WaterRefreshmentRow) actionRow);
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return out;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL delete">
	@Override
	public boolean delete(Integer id) throws SQLException
	{
		boolean result = false;
		if (id != null
				&& id >= 0)
		{
			synchronized (getRows())
			{
				PreparedStatement st = Database.prepareStatement(SQL_DELETE);
				st.setInt(1, id);

				try
				{
					ActionRow actionRow = getByPrimaryKey(id);
					if (st.executeUpdate() == 1
							&& super.delete(id))
					{
						result = true;
						fireWaterRefreshmentRemoved((WaterRefreshmentRow) actionRow);
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="SQL get">
	@Override
	public Collection<ActionRow> getAll() throws SQLException
	{
		PreparedStatement st = Database.prepareStatement(SQL_GET_ALL);
		synchronized (getRows())
		{
			if (!_allCached)
			{
				try
				{
					ResultSet query = st.executeQuery();
					getRows().clear();

					while (query.next())
					{
						WaterRefreshmentRow waterRefreshmentRow = new WaterRefreshmentRow(query);
						getRows().put(waterRefreshmentRow.getPrimaryKeys(), waterRefreshmentRow);
					}
					_allCached = true;
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
			return getRows().values();
		}
	}

	@Override
	public ActionRow getByPrimaryKey(Integer id) throws SQLException
	{
		ActionRow row = null;
		if (id >= 0)
		{
			synchronized (getRows())
			{
				row = getRows().get(id);
				if (row != null || _allCached)
				{
					return row;
				}

				PreparedStatement st = Database.prepareStatement(SQL_GET_BY_ID);
				st.setInt(1, id);

				try
				{
					ResultSet query = st.executeQuery();

					if (query.next())
					{
						row = new WaterRefreshmentRow(query);
						getRows().put(row.getPrimaryKeys(), row);
					}
				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return row;
	}

	@Override
	public ArrayList<ActionRow> getByAquariumId(int ID) throws SQLException
	{
		ArrayList<ActionRow> out = new ArrayList<ActionRow>();
		synchronized (getRows())
		{
			if (_allCached)
			{
				for (ActionRow actionRow : getRows().values())
				{
					if (actionRow.getAquarium().getId() == ID)
					{
						out.add(actionRow);
					}
				}
			}
			else
			{
				PreparedStatement st = Database.prepareStatement(SQL_GET_BY_AQUARIUM_ID);
				st.setInt(1, ID);

				try
				{
					ResultSet query = st.executeQuery();

					while (query.next())
					{
						WaterRefreshmentRow waterRefreshmentRow = new WaterRefreshmentRow(query);
						out.add(waterRefreshmentRow);
						getRows().put(waterRefreshmentRow.getPrimaryKeys(), waterRefreshmentRow);
					}

				}
				catch (SQLException ex)
				{
					throw ex;
				}
				finally
				{
					st.close();
				}
			}
		}
		return out;
	}
	//</editor-fold>
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Events">
	//<editor-fold defaultstate="collapsed" desc="Added">
	@Override
	public void addWaterRefreshmentAddedEventListener(
			WaterRefreshmentAddedListener listener)
	{
		synchronized (_waterRefreshmentAddedListeners)
		{
			_waterRefreshmentAddedListeners.add(listener);
		}
	}

	@Override
	public void removeWaterRefreshmentAddedEventListener(
			WaterRefreshmentAddedListener listener)
	{
		synchronized (_waterRefreshmentAddedListeners)
		{
			_waterRefreshmentAddedListeners.remove(listener);
		}
	}

	private void fireWaterRefreshmentAdded(WaterRefreshmentRow row)
	{
		synchronized (_waterRefreshmentAddedListeners)
		{
			for (WaterRefreshmentAddedListener listener : _waterRefreshmentAddedListeners)
			{
				listener.waterRefreshmentAdded(row);
			}
		}
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Removed">
	@Override
	public void addWaterRefreshmentRemovedEventListener(
			WaterRefreshmentRemovedListener listener)
	{
		synchronized (_waterRefreshmentRemovedListeners)
		{
			_waterRefreshmentRemovedListeners.add(listener);
		}
	}

	@Override
	public void removeWaterRefreshmentRemovedEventListener(
			WaterRefreshmentRemovedListener listener)
	{
		synchronized (_waterRefreshmentRemovedListeners)
		{
			_waterRefreshmentRemovedListeners.remove(listener);
		}
	}

	private void fireWaterRefreshmentRemoved(WaterRefreshmentRow row)
	{
		synchronized (_waterRefreshmentRemovedListeners)
		{
			for (WaterRefreshmentRemovedListener listener : _waterRefreshmentRemovedListeners)
			{
				listener.waterRefreshmentRemoved(row);
			}
		}
	}
	//</editor-fold>
	//</editor-fold>
}
